package com.shareppy.holidays.co.activities;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.shareppy.holidays.co.HolidaysWidgetProvider;
import com.shareppy.holidays.co.R;
import com.shareppy.nomrnaui.NormanActivity;
import com.shareppy.nomrnaui.annotations.Layout;
import com.shareppy.nomrnaui.annotations.View;

@Layout(R.layout.widget_config_layout)
public class ConfigActivity extends NormanActivity {

	private int widgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

	@View(R.id.update_rate)
	private EditText update_rate;

	@View(R.id.save_preferences)
	private Button save_preferences;

	@Override
	protected void on_create(Bundle savedInstanceState) {
		setResult(RESULT_CANCELED);

		save_preferences.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(android.view.View view) {
				final Context context = getActivity();
				AppWidgetManager appWidgetManager = AppWidgetManager
						.getInstance(context);
				HolidaysWidgetProvider.updateWidget(context, appWidgetManager,
						widgetId);

//				Intent intent = new Intent(
//						HolidaysWidgetProvider.HOLIDAYSWIDGET_UPDATE);
//				PendingIntent pendingIntent = PendingIntent.getBroadcast(
//						context, 0, intent, 0);
//				AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//				Calendar calendar = Calendar.getInstance();
//				calendar.setTimeInMillis(System.currentTimeMillis());
//				calendar.add(Calendar.SECOND, 10);
//				alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
//						calendar.getTimeInMillis(), 20 * 1000, pendingIntent);
//				HolidaysWidgetProvider.setAlarmManager(alarmManager);
//				HolidaysWidgetProvider.setPendingIntent(pendingIntent);

				Intent resultValue = new Intent();
				resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
						widgetId);
				setResult(RESULT_OK, resultValue);
				finish();
			}
		});

		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
		}
		if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
			finish();
		}
	}

}
